package com.q.tqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.model.MineRedInfo;
import com.q.tqb.view.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MineGrabRedAdapter extends RecyclerView.Adapter<MineGrabRedAdapter.ViewHolder> {
    private ArrayList<MineRedInfo> mRedPersonDatas;
    private Context mContext;


    public MineGrabRedAdapter(Context context, ArrayList<MineRedInfo> mRedPersonDatas) {

        this.mRedPersonDatas = mRedPersonDatas;
        this.mContext = context;


    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mine_grab_red, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        MineRedInfo mineRedInfo = mRedPersonDatas.get(position);

        holder.mTvMoney.setText(mineRedInfo.getGet_money() + "元");

        holder.mTvTitle.setText(mineRedInfo.getPacket_title());
        holder.mTvTime.setText(mineRedInfo.getCreate_time());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onClick(position);
            }
        });

        Glide
                .with(mContext)
                .load(mineRedInfo.getHeader())
                .error(R.drawable.ic_head)
                .placeholder(R.drawable.ic_head)
                .dontAnimate()
                .into(holder.mIvHead);
    }

    @Override
    public int getItemCount() {
        return mRedPersonDatas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_money)
        TextView mTvMoney;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.iv_head)
        CircleImageView mIvHead;


        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}